package com.beep.erp.model.home.classify;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.BizGoodsListBean;
import com.beep.erp.common.bean.ClassifyBean;
import com.beep.erp.common.bean.TransferLogBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.ClearEditText;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultFooter;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.common.widget.tips.TipsHelper;
import com.beep.erp.model.goodsdetail.GoodsDetailActivity;
import com.beep.erp.model.home.HomeAdapter;
import com.beep.erp.model.home.HomeContract;
import com.beep.erp.model.home.HomeModel;
import com.beep.erp.model.home.HomePresenter;
import com.beep.erp.model.transfer.TransferLogListAdapter;
import com.beep.erp.model.transfer.TransferLogListContract;
import com.beep.erp.model.transfer.TransferLogListModel;
import com.beep.erp.model.transfer.TransferLogListPresenter;
import com.beep.erp.model.transfer.pallet.PalletGoodsActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: TransferActivity
 * description: 扫描
 * Author: Jolly
 * Date: 2018/9/3
 */
public class ClassifyActivity extends ToolbarActivity<ClassifyPresenter, HomeModel> implements HomeContract.View, ClassifyAdapter.OnItemClickLitener, RefreshView.OnFreshListener {
    private static final String CLASSIFY = "classify";


    @BindView(R.id.iv_scan)
    ImageView ivScan;
    @BindView(R.id.appbatlayout)
    AppBarLayout appbatlayout;
    @BindView(R.id.rl_instock)
    RecyclerView rlInstock;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;
    @BindView(R.id.et_goods_search)
    EditText etSearch;
    @BindView(R.id.iv_search)
    ImageView ivSearch;

    TipsHelper mTipsHelper;
    private int mPageNum = 1;
    private ArrayList<BizGoodsListBean.BizGoodsBean> mListBeen = new ArrayList<>();
    private ClassifyAdapter mClassifyAdapter;
    private String searchKey;

    public static void start(Context context, String classify) {
        Intent intent = new Intent(context, ClassifyActivity.class);
        intent.putExtra(CLASSIFY, classify);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_classify;
    }

    @Override
    protected void initView() {
        getTvTitle().setText(R.string.main_tab_name_order_query);

        mTipsHelper = createTipsHelper(superRefresh);
        //refreshView下拉刷新 上拉加载
        superRefresh.setListener(this);
        superRefresh.setHeader(new DefaultHeader(this, true));
        superRefresh.setFooter(new DefaultFooter(this, true));
        //mInviteAdapter recycleview适配器
        mClassifyAdapter = new ClassifyAdapter(this, mListBeen);
        //设置为纵向
        rlInstock.setLayoutManager(new LinearLayoutManager(this));
        //必写
        rlInstock.setHasFixedSize(true);
        rlInstock.setAdapter(mClassifyAdapter);
        mClassifyAdapter.setOnItemClickLitener(this);

        ivSearch.setOnClickListener(view -> {
            searchData();
        });

        etSearch.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                searchData();
            }
            return false;
        });
    }

    private void searchData() {
        hideSoftKeyboard();
        searchKey = etSearch.getText().toString();
        if (ToolsUtils.isEmpty(searchKey)) searchKey = "all";
        getFirstData();
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        mTipsHelper.hideLoading();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideEmpty();
        mTipsHelper.hideError();
        superRefresh.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        if (ToolsUtils.isEmpty(message)) return;
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> {
                mTipsHelper.hideError();
                getDate();
            });
        } else {
            ToastUtils.showShort(message);
        }
    }

    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {
        mPageNum++;
        getTransLogData();
    }

    @Override
    public void getDate() {
        getFirstData();
    }

    private void getFirstData() {
        mListBeen.clear();
        mPageNum = 1;
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideError();
        mTipsHelper.showLoading(true);
        getTransLogData();
    }

    @Override
    public void getBizGoods(BizGoodsListBean bizGoodsListBean) {
        if (ToolsUtils.isEmptyList(bizGoodsListBean.getBizGoodsList()) && mPageNum > 1) {
            ToastUtils.showShort(getString(R.string.no_data));
            return;
        }
        mListBeen.addAll(bizGoodsListBean.getBizGoodsList());
        mClassifyAdapter.notifyDataSetChanged();
        if (bizGoodsListBean.getBizGoodsList().size() == 0) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

    @Override
    public void getGoodsClassify(ClassifyBean classifyBean) {

    }

    private void getTransLogData() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("pageSize", Constants.PAGE_SIZE + "");
        map.put("pageNo", mPageNum + "");
        map.put("classifyId", getIntent().getStringExtra(CLASSIFY));
        map.put("searchKey", ToolsUtils.isEmpty(searchKey) ? "all" : searchKey);
        mPresenter.getBizGoods(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", Constants.PAGE_SIZE, mPageNum, getIntent().getStringExtra(CLASSIFY), ToolsUtils.isEmpty(searchKey) ? "all" : searchKey);
    }

    @Override
    public void onItemClick(View view, int position) {
        GoodsDetailActivity.start(this, mListBeen.get(position).getGoodsId());
    }
}
